'use strict';
app.controller('ordersHistoryCtrl',['$scope','utilService','dataHistoryService','toaster','$filter','$modal',
    function($scope,utilService,dataHistoryService,toaster,$filter,$modal){

    $scope.ordersHistory={};
    $scope.query = function(){
        if($scope.ordersHistory.startTime != null){
            $scope.ordersHistory.startTime= $filter("date")($scope.ordersHistory.startTime, "yyyy-MM-dd");
        }
        if($scope.ordersHistory.endTime != null){
            $scope.ordersHistory.endTime= $filter("date")($scope.ordersHistory.endTime, "yyyy-MM-dd");
        }
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'orders/data',
            params:$scope.ordersHistory
        }).then(function(result){
        });
    };
    $scope.query();

    $scope.getShopList = function(){
        $scope.shopSelectList=[];
        dataHistoryService.getShopList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shopSelectList = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.getShopList();

    //导出订单及商品历史报表
    $scope.exportData = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/data/orders_goods_export.html',
            controller: 'ordersExportHistoryCtrl',
            size: 'sm',
            scope:$scope
        });

        modalInstance.result.then(function (selectedItem) {
            console.log($scope.selected = selectedItem);
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('goodsHistoryCtrl',['$scope','dataHistoryService','$stateParams', function($scope,dataHistoryService,$stateParams){

    $scope.goodsHistory={
        ordersDataHistoryId:$stateParams.id
    };
    $scope.query = function(){
        dataHistoryService.query($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsHistory = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.query();

}]);

//导出订单及商品历史数据模态框Controller
app.controller('ordersExportHistoryCtrl', ['$scope', '$modalInstance','dataHistoryService','$filter','$cookies',
    function($scope, $modalInstance,dataHistoryService,$filter,$cookies) {
        $scope.AllowToReport=true;

        $scope.ordersAndGoodsVO={
            sessionId:$cookies.sessionId
        };
        $scope.changStart=function(){
            $scope.ordersAndGoodsVO.maxTime = $scope.ordersAndGoodsVO.start.getTime()+60*60*1000*24*90;
        };

        $scope.changEnd=function(){
            $scope.ordersAndGoodsVO.minTime = $scope.ordersAndGoodsVO.end.getTime()-60*60*1000*24*90;
        };

        $scope.ok=function(){
            if(!$scope.AllowToReport){
                return;
            }
            $scope.AllowToReport=false;
            $scope.ordersAndGoodsVO.start=$filter('date')($scope.ordersAndGoodsVO.start,'yyyy-MM-dd');
            $scope.ordersAndGoodsVO.end=$filter('date')($scope.ordersAndGoodsVO.end,'yyyy-MM-dd');
            dataHistoryService.exportData($scope);
            $modalInstance.dismiss('success');
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);